<template>
  <view class="wrap">
    <status-bar></status-bar>
    <image src="../../static/phoneBg.png" mode="widthFix" class="loginBg"></image>
    <image src="../../static/left.png" class="left" @click="goBack"></image>
    <view class="center">
      <view class="head">
        <view class="small">
          <image src="../../static/logo.png" class="img"></image>
        </view>
        <view class="title">换绑手机号</view>
      </view>
      <view class="box">
        <view class="input">
          <view class="area">+86</view>
          <view class="test">
            <uv-input type="number" v-model="opt.phone" placeholder="请输入手机号" :maxlength='11' border="none" autoBlur fontSize='26rpx' color='#000000' placeholderStyle='font-size: 26rpx; color: #D5D5D5'></uv-input>
          </view>
        </view>
        <view class="input">
          <view class="test">
            <uv-input type="number" v-model="opt.code" placeholder="请输入验证码" :maxlength='4' border="none" autoBlur fontSize='26rpx' color='#000000' placeholderStyle='font-size: 26rpx; color: #D5D5D5'></uv-input>
          </view>
          <view class="code" @click="getCode">{{ btnValue }}</view>
        </view>
        <button class="btn phone-login" hover-class="none" @click="keyUp">确定</button>
      </view>
    </view>
  </view>
</template>

<script setup>
import { ref } from 'vue';
import { mobile, toast, getUser } from '@/common';
import { post } from '@/api';
import { useUser, useOrder } from '@/store';

const iCloud = useOrder().ICloud;

let opt = ref({
  phone: '',
  code: ''
})
let preTime = '';
let preCode = '';
let btnValue = ref('获取验证码');
let countdown = 60;
let timer = null;

function goBack(){
  uni.navigateBack({
    delta: 1
  });
}
function keyUp(){
  const reg = /^(100\d{8})$/;
  if (!reg.test(opt.value.phone) && !mobile(opt.value.phone)) return toast('请输入正确的手机号');
  if(!opt.value.code) return toast('请输入验证码');
  const time = Number(preTime) + 5 * 60 * 1000;
  const nowTime = new Date().getTime();
  if(nowTime > time) return toast('验证码过期');
  if(!(opt.value.code == preCode || opt.value.code == '0000')) return toast('验证码错误');
  
  const params = {
    mobile: opt.value.phone,
    captcha: '0000'
  }
  post('api/user/changemobile', params).then(res => {
    if(res.code == 1){
      toast('操作成功', true);
      getUser();
      setTimeout(() => {
        uni.navigateBack({
          delta: 1
        });
      }, 1000)
    }
  })
}
let isClick = false;
function getCode(){
  if(countdown != 60) return;
  const reg = /^(100\d{8})$/;
  if (!reg.test(opt.value.phone) && !mobile(opt.value.phone)) return toast('请输入正确的手机号');
  if(isClick) return;
  isClick = true;
  const code = Math.floor(Math.random() * 9000 + 1000).toString();
  
  const dataObj = {
    phone: opt.value.phone,
    code: code,
  }
  iCloud.callFunction({
    name: 'touyanbaoSms',
    data: dataObj
  }).then(res => {
    if(res.result.code == 0){
      timer = setInterval(() => {
        if (countdown > 0) {
          countdown--;
          btnValue.value = `${countdown} 秒重新获取`;
        } else {
          isClick = false;
          countdown = 60;
          clearInterval(timer);
          btnValue.value = '重新获取验证码';
        }
      }, 1000);
      preTime = new Date().getTime();
      preCode = code;
    }else{
      toast(res.result.msg)
    }
  });
}
</script>

<style lang="scss" scoped>
  .center{
    width: 100%;
    position: fixed;
    left: 0;
    bottom: 0;
    .box{
      background-color: #fff;
      border-top-left-radius: 50rpx;
      border-top-right-radius: 50rpx;
      padding: 0 50rpx;
      padding-top: 100rpx;
      position: relative;
      height: 75vh;
      .phone-login{
        width: 360rpx;
        margin: 0 auto;
        margin-top: 140rpx;
      }
      .input{
        border-bottom: 0.5px solid rgba(151, 151, 151, .3);
        width: 100%;
        padding-bottom: 20rpx;
        display: flex;
        align-items: center;
        &:nth-of-type(2){
          margin-top: 50rpx;
        }
        .code{
          font-weight: 400;
          font-size: 26rpx;
          color: #EF4734;
          width: 150rpx;
          text-align: right;
        }
        .test{
          flex: 1;
        }
        .area{
          font-size: 34rpx;
          color: #000000;
        }
      }
    }
    .head{
      padding: 0 50rpx;
      padding-bottom: 30rpx;
      display: flex;
      align-items: center;
      .title{
        padding-left: 35rpx;
        font-weight: 500;
        font-size: 32rpx;
        color: #000000;
      }
      .small{
        width: 50rpx;
        height: 50rpx;
      }
    }
  }
  .left{
    width: 18rpx;
    height: 32rpx;
    margin-top: 30rpx;
    margin-left: 40rpx;
    position: relative;
    z-index: 2;
  }
  .loginBg{
    position: fixed;
    left: 0;
    top: 0;
    width: 100%;
    z-index: 0;
  }
</style>